﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Data.SqlClient;

namespace Model.Account
{
    public class Account
    {
        string conn = ConfigurationManager.ConnectionStrings["OnlineFloralShopConnection"].ConnectionString;
        //ciphertext data
        static string getMD5Hash(string input)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] pass = Encoding.UTF8.GetBytes(input);
            string strPassword = Encoding.UTF8.GetString(md5.ComputeHash(pass));
            return strPassword;
        }

        static bool verifyMD5Hash(string input, string hash)
        {
            string hashOffInput = getMD5Hash(input);
            StringComparer compare = StringComparer.OrdinalIgnoreCase;
            if (0 == compare.Compare(hashOffInput, hash))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //compare old password 
        /*
        public bool CheckAccount(string username, string oldpass)
        {
          //  string newpassword = getMD5Hash(newpass);
            string commandstring = "Select * from tblmembership where UserName='" + username + "'";
         //   string update = "Update tblmembership Set Password=" + newpass + "where UserName='" + username + "'";

            SqlConnection con = new SqlConnection(conn);
            SqlCommand cmd = new SqlCommand(commandstring, con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            con.Open();

          //  string hasoffinput = getMD5Hash(oldpass);
            if ((dt.Rows.Count == 0 || dt.Rows[0][3].ToString() != oldpass))
            {
                return true;
            }
           
            con.Close();
        }
        */
        public  string CreateRandomPassword(int PasswordLength)
        {
            string _allowedChars = "abcdefghijkmnopqrstuvwxyz0123456789";
            Random randNum = new Random();
            char[] chars = new char[PasswordLength];
            int allowedCharCount = _allowedChars.Length;

            for (int i = 0; i < PasswordLength; i++)
            {
                chars[i] = _allowedChars[(int)((_allowedChars.Length) * randNum.NextDouble())];
            }
            return new string(chars); 
        }
        /*
        public static void SendMail(string to, string strSubject, string strContent)
        {
            MailMessage mail = new MailMessage();

            mail.To.Add(to);
            mail.Subject = strSubject;
            mail.IsBodyHtml = true;
            mail.Body = strContent;
            mail.From = new MailAddress("minhtn1982@gmail.com");
            try
            {
                SmtpClient client = new SmtpClient();
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.EnableSsl = true;
                client.Host = "smtp.gmail.com";
                client.Port = 587;
                // khai báo tài kho?n và mat kh?u g?i mail 
                NetworkCredential credentials = new NetworkCredential("123456789@gmail.com", "123456");
                client.UseDefaultCredentials = false;
                client.Credentials = credentials;
                client.Send(mail); // thuc hien gui mail 
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Loi " + ex.ToString());
                return false;
            } 
 
        }
      */
    }
}
